package cn.tedu.adminsystem.webapi.controller;

import cn.tedu.adminsystem.service.NoticeService;
import cn.tedu.community.commons.pojo.notice.dto.NoticeAddNewDTO;
import cn.tedu.community.commons.pojo.notice.dto.NoticeUpdateDTO;
import cn.tedu.community.commons.pojo.notice.vo.AdmNoticeListItemVO;
import cn.tedu.community.commons.pojo.notice.vo.AdmNoticeStandardVO;
import cn.tedu.community.commons.restful.JsonPage;
import cn.tedu.community.commons.restful.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

/**
 * 公告Controller
 */
@RestController
@Slf4j
@Validated
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService service;

    @PreAuthorize("hasAnyAuthority('/com/notice/add-new')")
    @PostMapping("/addNew")
    public JsonResult<Void> addNew(@Valid NoticeAddNewDTO newDTO) {
        log.debug("添加公告参数是：{}", newDTO);
        service.addNew(newDTO);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAnyAuthority('/com/notice/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        log.debug("需要删除的公告id是：{}", id);
        service.deleteById(id);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAnyAuthority('/com/notice/delete')")
    @PostMapping("/deleteIds")
    public JsonResult<Void> deleteByIds(@RequestBody Long[] ids) {
        service.deleteByIds(ids);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAnyAuthority('/com/notice/update')")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable Long id, @Valid NoticeUpdateDTO updateDTO) {
        log.debug("需要修改公告的id是：{},修改参数是：{}", id, updateDTO);
        service.update(id, updateDTO);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAnyAuthority('/com/notice/read')")
    @GetMapping("/{id:[0-9]+}/admNoticeStandard")
    public JsonResult<AdmNoticeStandardVO> getAdmNoticeStandardById(@PathVariable Long id) {
        AdmNoticeStandardVO standardVO = service.getAdmNoticeStandardById(id);
        return JsonResult.ok(standardVO);
    }

    @PreAuthorize("hasAnyAuthority('/com/notice/read')")
    @GetMapping("/{page:[0-9]+}/{pageSize:[0-9]+}/admNoticeList")
    public JsonResult<JsonPage<AdmNoticeListItemVO>> getAdmNoticeList(@PathVariable Integer page, @PathVariable Integer pageSize) {
        log.debug("开始展示管理员页面公告");
        JsonPage<AdmNoticeListItemVO> admNoticeList = service.getAdmNoticeList(page, pageSize);
        log.debug("管理员数据是：{}",admNoticeList);
        return JsonResult.ok(admNoticeList);
    }

    @PreAuthorize("hasAnyAuthority('/com/notice/read')")
    @GetMapping("/{page:[0-9]+}/{pageSize:[0-9]+}/title={title}/AdmSearchNoticeList")
    public JsonResult<JsonPage<AdmNoticeListItemVO>> getAdmNoticeListByTitle(@Pattern(regexp = "([\\w\\W]{1,20}$)",
            message = "请输入您发布的1~20个字符的模糊标题关键字!") @PathVariable String title,
                                                                             @PathVariable Integer page, @PathVariable Integer pageSize) {
        log.debug("需要查询的标题是：{}", title);
        JsonPage<AdmNoticeListItemVO> admNoticeListByTitle = service.getAdmNoticeListByTitle(title, page, pageSize);
        return JsonResult.ok(admNoticeListByTitle);
    }
}
